package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Admin;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.AdminService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminApiController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/list")
    public R<List<Admin>> findAll() {
        List<Admin> admins = adminService.getAll();
        return RUtil.success(admins);
    }

    @GetMapping("/{id}")
    public R<Admin> findById(@PathVariable int id) {
        Admin admin = adminService.findById(id);
        return RUtil.success(admin);
    }

    @GetMapping("/findByUsername")
    public R<List<Admin>> findByUsername(@RequestParam String name) {
        return RUtil.success(adminService.findByName(name));
    }

    @PostMapping("/add")
    public R<Admin> add(@RequestBody Admin admin) {
        return RUtil.success(adminService.add(admin));
    }

    @PutMapping("/update")
    public R<Admin> update(@RequestBody Admin admin) {
        return RUtil.success(adminService.update(admin));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        adminService.delete(id);
        return RUtil.success();
    }

    @GetMapping("/validateUser")
    public R validateNoAndPassword(String no, String password) throws Exception {
        return RUtil.success(adminService.validateNoAndPassword(no, password));
    }

    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        try {
            adminService.deleteBatch(ids);
            return RUtil.success();
        } catch (Exception e) {
            return RUtil.error(REnum.DELETE_ERR.getCode(), "批量删除失败");
        }
    }

    @PostMapping("/getbypage")
    public R<Page<Admin>> getByPage(@RequestBody QueryObj<Admin> qObj) {
        System.out.println("接收到的请求数据: " + qObj); // 调试输出请求数据

        // 按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Integer pageIndex = 0;
        Integer pageSize = 10; // 一页10条数据

        if (qObj == null) {
            // admin示例为空就直接调用分页
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Admin> admins = adminService.getAll(pageable);
            return RUtil.success(admins.getContent(), admins.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1; // 至少是1页
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Admin) { // 示例查询
                Admin admin = (Admin) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching() // 生成示例匹配对象
                        // 匹配条件
                        .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.contains())
                        // 忽略空值
                        .withIgnoreNullValues();

                Example<Admin> example = Example.of(admin, matcher);
                Page<Admin> adminPage = adminService.getAll(example, pageable);

                return RUtil.success(adminPage.getContent(), adminPage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}